#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QScreen>
#include "Personal.h"
#include <QMap>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QScreen *screen = QGuiApplication::primaryScreen();
    this->screenGeometry = screen->geometry();
    qDebug()<<"uuuuuuuuuuu  "<<"w:"<<this->screenGeometry.width()<<"   h:"<<this->screenGeometry.height()<<endl;
    loginPage=new LoginPage(this->screenGeometry);
    this->loginPage->show();

    mTCPClient=new TCPClient;
    this->mTCPClient->moveToThread(&this->TCPClientThread);
    this->TCPClientThread.start();
    QMetaObject::invokeMethod(this->mTCPClient,"init",Qt::AutoConnection);
    connect(&mTCPClient->mTask,&Task::zhuCeStatus,this->loginPage,&LoginPage::zhuCeStatus);
    connect(loginPage,&LoginPage::connectServer,mTCPClient,&TCPClient::startLanding);
    connect(loginPage,&LoginPage::disConnectServer,mTCPClient,&TCPClient::disConnectServer);
    connect(mTCPClient,&TCPClient::connectStatus,this,&MainWindow::receiveTimeout);
    connect(&mTCPClient->getTask(),&Task::loginSucceed,this,&MainWindow::creatMainMenu);
    connect(this->loginPage,&LoginPage::sendServerData,this->mTCPClient,&TCPClient::write);
//    this->myMainMenu=new MainMenu(this->screenGeometry);
//    this->myMainMenu->show();
  //  myRegistration=new Registration(this->screenGeometry);
//    Personal *per=new Personal(this->screenGeometry,"",0,"","");
//    per->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
}

void MainWindow::receiveTimeout(QString reason)
{
    if(reason=="网络异常")
    {
        this->loginPage->setServerStatus(false);
    }
    else if(reason=="连接成功")
    {
        this->loginPage->setServerStatus(true);
    }
    else
    {
        QMessageBox *box=new QMessageBox;
        box->warning(nullptr,"",reason);
        box->exec();
        delete box;
    }
}

void MainWindow::creatMainMenu()
{//phone:13517045934;name:caojinpeng;pass:123456;sex:0;id:360481199905260432;blood:O;
    this->myMainMenu=new MainMenu(this->screenGeometry,this->loginPage->getUser()->text(),this->loginPage->getPass()->text());
    this->loginPage->close();
    delete this->loginPage;
    connect(&this->mTCPClient->getTask(),&Task::sendMainMenuData,this->myMainMenu,&MainMenu::receiveServerData_slot);
    connect(this->myMainMenu,&MainMenu::toServerSend_signals,this->mTCPClient,&TCPClient::write);
    this->myMainMenu->show();
}
